﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace HousingDistribution
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            DBConfInfo info = DBConf.Instance().ReadData();
            DBProc.Instance().SetServerInfo(info.server, info.db, info.port, info.uid, info.pwd);

            DataDictionary.Instance().Read("DataDicConf.xml");

            //读取房子信息
            Thread t = new Thread(DataDictionary.Instance().ReadHousingInfoFromMySql);
            t.Start();

            //更新工龄
            //UpdateServiceLength serviceLengthUpdator = new UpdateServiceLength(DateTime.Now);
            //t = new Thread(serviceLengthUpdator.Proc);
            //t.Start();
        }
        
        private void DataFromExcelMenuItem_Click(object sender, EventArgs e)
        {
            ProcExcel();
        }

        private void ShowMdiChild(Form child)
        {
            child.MdiParent = this;
            child.Show();
        }

        private void ExitMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DBConfMenuItem_Click(object sender, EventArgs e)
        {
            FormDBConf dbConf = new FormDBConf();
            ShowMdiChild(dbConf);
        }

        private void AboutMenuItem_Click(object sender, EventArgs e)
        {
            AboutMe aboutMe = new AboutMe();
            ShowMdiChild(aboutMe);
        }

        private void HousingDistributionMenuItem_Click(object sender, EventArgs e)
        {
            FormHousingDis housingDistrition = new FormHousingDis();
            ShowMdiChild(housingDistrition);
        }

        private void toolStripButtonExcel2DB_Click(object sender, EventArgs e)
        {
            ProcExcel();
        }
        private void toolStripButtonProc_Click(object sender, EventArgs e)
        {
            FormHousingDis housingDistrition = new FormHousingDis();
            ShowMdiChild(housingDistrition);
        }
        private void toolStripButtonDBConf_Click(object sender, EventArgs e)
        {
            FormDBConf dbConf = new FormDBConf();
            ShowMdiChild(dbConf);
        }

        private void toolStripButtonAboutMe_Click(object sender, EventArgs e)
        {
            AboutMe aboutMe = new AboutMe();
            ShowMdiChild(aboutMe);
        }

        private void toolStripButtonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ProcExcel()
        {
            //弹出文件选择对话框，选择需要的数据文件
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = this.GetType().Assembly.Location;
            openFileDialog.Filter = "Excel文件|*.xls*";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                //如果选中文件，则对文件进行处理（首先进行文件内容预览）
                FormExcelPreview previewWin = new FormExcelPreview(openFileDialog.FileName);
                ShowMdiChild(previewWin);
            }　
        }

        private void ToolStripMenuItemPersonDetail_Click(object sender, EventArgs e)
        {
            FormPersonDetailsNoHousingTab tabForm = new FormPersonDetailsNoHousingTab();
            ShowMdiChild(tabForm);
        }

        private void toolStripButtonPersonDetails_Click(object sender, EventArgs e)
        {
            FormPersonDetailsNoHousingTab tabForm = new FormPersonDetailsNoHousingTab();
            ShowMdiChild(tabForm);
        }

        private void PersonDetailsSingleWinToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //FormPersonDetailsTab tabForm = new FormPersonDetailsTab();
            //ShowMdiChild(tabForm);
        }

        private void DeadlineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormDeadlineSet form = new FormDeadlineSet();
            ShowMdiChild(form);
        }

        //sql数据文件导入。首先要手工创建数据库mydb才能导入数据表。导入过程先删除现有数据表，重新创建。
        private void toolStripButtonDBimport_Click(object sender, EventArgs e)
        {
            ProcDBimport();
        }

        private void DBImportMenuItem_Click(object sender, EventArgs e)
        {
            ProcDBimport(); 
        }

        private void ProcDBimport()
        {
            //弹出文件选择对话框，选择需要的数据文件
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = this.GetType().Assembly.Location;
            openFileDialog.Filter = "sql文件|*.sql";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                //如果选中文件，则对文件进行处理（首先进行文件内容预览）
                //FormExcelPreview previewWin = new FormExcelPreview(openFileDialog.FileName);
                //ShowMdiChild(previewWin);

                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName = "cmd.exe";
                psi.Arguments = "/c mysql -u root mydb < " + openFileDialog.FileName;
                Process.Start(psi);
                //MessageBox.Show(openFileDialog.FileName);
            }
        }

        //sql数据文件导出。
        private void toolStripButtonDBexport_Click(object sender, EventArgs e)
        {
            ProcDBexport();
        }

        private void DBExportMenuItem_Click(object sender, EventArgs e)
        {
            ProcDBexport();
        }

        private void ProcDBexport()
        {
            SaveFileDialog backupDB = new SaveFileDialog();
            backupDB.Filter = "sql files|*.sql";
            backupDB.FilterIndex = 0;
            backupDB.RestoreDirectory = true;
            backupDB.CreatePrompt = true;
            backupDB.Title = "导出文件保存路径";
            //backupDB.ShowDialog();

            if (backupDB.ShowDialog() == DialogResult.OK)
            {
                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName = "cmd.exe";
                psi.Arguments = "/c mysqldump -uroot mydb > " + backupDB.FileName;
                Process.Start(psi);
                MessageBox.Show("数据库导出完成！");
            }
        }

    }
}
